export GLOG_logtostderr=1
export GLOG_v=3
chunk_size=64

# shellcheck disable=SC2154
jit_model_path=/home/work_nfs15/asr_data/ckpt/asr_online_system/jit_script_chunk_"${chunk_size}"_left_1024.pt
unit_path=/home/work_nfs15/asr_data/ckpt/asr_online_system/lang_char_bpe/tokens.txt

test_set=$1 # speechio_1 speechio_2 speechio_3 speechio_4 speechio_5 speechio_6 speechio_7 speechio_8 speechio_9 speechio_10 speechio_11 speechio_12 speechio_13 speechio_14 speechio_15 speechio_16 speechio_17 speechio_18 speechio_19 speechio_20 speechio_21 speechio_22 speechio_23 speechio_24 speechio
# shellcheck disable=SC2034
text_path=/home/work_nfs15/asr_data/data/asr_test_sets/${test_set}/text
scp_path=/home/work_nfs15/asr_data/data/asr_test_sets/${test_set}/wav.scp
# scp_path=wav_100.scp



# thres=0.000000001
thres=$2
echo "thres is $thres"
if [ $thres -eq 0 ]; then
  fst_dir=""
else
  fst_dir="/home/node54_tmpdata/xlgeng/code/gxl_ai_utils/eggs/cats_and_dogs/ngram_task/train_ngram/gxl_arpa_zoo/asr_poi_xian/thres_$thres/lang_test"
fi


output_dir=./decode_output/asr_poi_xian
mkdir -p $output_dir
output_path=$output_dir/test_"$test_set"_with_ngram_thres_$thres.text
output_path_wer=$output_dir/test_"$test_set"_with_ngram_thres_$thres.wer

fst_path=$fst_dir/TLG.fst
beam=32
dict_path=$fst_dir/words.txt
lattice_beam=5
max_active=7000
min_active=200
acoustic_scale=1.2
blank_skip_thresh=0.99
length_penalty=0.0

wfst_decode_opts=
if [ ! -z $fst_path ]; then
  wfst_decode_opts="--fst_path $fst_path"
  wfst_decode_opts="$wfst_decode_opts --beam $beam"
  wfst_decode_opts="$wfst_decode_opts --dict_path $dict_path"
  wfst_decode_opts="$wfst_decode_opts --lattice_beam $lattice_beam"
  wfst_decode_opts="$wfst_decode_opts --max_active $max_active"
  wfst_decode_opts="$wfst_decode_opts --min_active $min_active"
  wfst_decode_opts="$wfst_decode_opts --acoustic_scale $acoustic_scale"
  wfst_decode_opts="$wfst_decode_opts --blank_skip_thresh $blank_skip_thresh"
  wfst_decode_opts="$wfst_decode_opts --length_penalty $length_penalty"
#  echo $wfst_decode_opts > $dir/config
fi

if [ "$fst_str" == "" ]; then
    /home/work_nfs11/code/xlgeng/wenet_mine/runtime/libtorch/build/bin/decoder_main \
    --chunk_size -1 \
    --rescoring_weight 5.0 \
    --wav_scp $scp_path \
    --continuous_decoding false \
    --model_path $jit_model_path \
    --unit_path $unit_path \
    --result $output_path 2>&1 | tee log_with_ngram.txt

else
    /home/work_nfs11/code/xlgeng/wenet_mine/runtime/libtorch/build/bin/decoder_main \
      --chunk_size -1 \
      --rescoring_weight 5.0 \
      --wav_scp $scp_path \
      --continuous_decoding false \
      --model_path $jit_model_path \
      --unit_path $unit_path \
      $wfst_decode_opts \
      --result $output_path 2>&1 | tee log_with_ngram.txt

fi



echo '开始计算wer'
python tools/compute-wer.py --char=1 --v=1 \
  $text_path $output_path > $output_path_wer

tail -n 10 $output_path_wer
